<?php
/**
 * <pre>
 * DiabloStudios.Board v1.0
 * 
 * Index page of DiabloStudios.Board
 * </pre>
 * 
 * @copyright	(c) 2010 DiabloStudios
 * @license		GNU Affero GPL v3 or newer
 * @package		PHP-Fusion
 * @subpackage	DiabloStudios.Board
 * @link		http://diablostudios.pl
 * @since 		version 1.0
 * @version     $Id: index.php 1 2011-03-20 11:56:30Z Bartek $
 * 
 */


require_once DSB_LANGDIR . "forum_index.php";
require_once DSB_DIR . "includes/functions_forumlist.php";

$whosonline = '';
if ($dsb->settings['showwol'] != 0 && $dsb->usergroup['canviewonline'] != 0)
{
	// Get the online users.
	$timesearch = TIME_NOW - $dsb->settings['wolcutoff'];
	$comma = '';
	$query = dbquery("
		SELECT  o.online_lastactive, u.user_name, u2.uid, u2.invisible, u2.usergroup, u2.displaygroup
		FROM " . DB_ONLINE . " o
		LEFT JOIN " . DB_USERS . " u ON (o.online_user=u.user_id)
        LEFT JOIN " . DB_DSB_USERS . " u2 ON (o.online_user=u2.uid)
		WHERE o.online_lastactive > '$timesearch'
		ORDER BY u.user_name ASC, o.online_lastactive DESC
	");

	$forum_viewers = array();
	$membercount = 0;
	$onlinemembers = '';
	$guestcount = 0;
	$anoncount = 0;
	$doneusers = array();

	// Fetch spiders
	$spiders = $cache->read("spiders");

	// Loop through all users.
	while ($user = dbarray($query))
	{
		// Create a key to test if this user is a search bot.
		$botkey = my_strtolower(str_replace("bot=", '', $user['sid']));

		// Decide what type of user we are dealing with.
		if ($user['uid'] > 0)
		{
			// The user is registered.
			if ($doneusers[$user['uid']] < $user['online_lastactive'] || !$doneusers[$user['uid']])
			{
				// If the user is logged in anonymously, update the count for that.
				if ($user['invisible'] == 1)
				{
					++$anoncount;
				}
				++$membercount;
				if ($user['invisible'] != 1 || $dsb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $dsb->user['uid'])
				{
					// If this usergroup can see anonymously logged-in users, mark them.
					if ($user['invisible'] == 1)
					{
						$invisiblemark = "*";
					}
					else
					{
						$invisiblemark = '';
					}

					// Properly format the username and assign the template.
					$user['username'] = format_name($user['user_name'], $user['usergroup'], $user['displaygroup']);
					$user['profilelink'] = profile_link($user['uid'], $user['username'], '');
					eval("\$onlinemembers .= \"" . $templates->get("index_whosonline_memberbit", 1, 0) . "\";");
					$comma = $locale['comma'];
				}
				// This user has been handled.
				$doneusers[$user['uid']] = $user['time'];
			}
		} elseif (my_strpos($user['sid'], "bot=") !== false && $spiders[$botkey])
		{
			// The user is a search bot.
			$onlinemembers .= $comma . format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
			$comma = $locale['comma'];
			++$botcount;
		}
		else
		{
			// The user is a guest.
			++$guestcount;
		}

		if ($user['location1'])
		{
			$forum_viewers[$user['location1']]++;
		}
	}

	// Build the who's online bit on the index page.
	$onlinecount = $membercount + $guestcount + $botcount;

	if ($onlinecount != 1)
	{
		$onlinebit = $locale['online_online_plural'];
	}
	else
	{
		$onlinebit = $locale['online_online_singular'];
	}
	if ($membercount != 1)
	{
		$memberbit = $locale['online_member_plural'];
	}
	else
	{
		$memberbit = $locale['online_member_singular'];
	}
	if ($anoncount != 1)
	{
		$anonbit = $locale['online_anon_plural'];
	}
	else
	{
		$anonbit = $locale['online_anon_singular'];
	}
	if ($guestcount != 1)
	{
		$guestbit = $locale['online_guest_plural'];
	}
	else
	{
		$guestbit = $locale['online_guest_singular'];
	}
	$locale['online_note'] = ds_sprintf($locale['online_note'], my_number_format($onlinecount), $onlinebit, $dsb->settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);

	$usergroups_legend = $cache->read("usergroups");
	$legend = "";
	foreach ($usergroups_legend as $id => $group)
	{
		if ($id != 1 && $id != 5)
		{
			$legend .= $comma . "[".format_name($group['title'], $group['gid'])."]";
			$comma = " ";
		}
	}
	eval("\$whosonline = \"" . $templates->get("index_whosonline") . "\";");
}

// Build the forum statistics to show on the index page.
if ($dsb->settings['showindexstats'] != 0)
{
	// First, load the stats cache.
	$stats = $cache->read("stats");

	// Check who's the newest member.
	if (!$stats['lastusername'])
	{
		$newestmember = "no-one";
	}
	else
	{
		$newestmember = profile_link($stats['lastusername'], $stats['lastuid']);
	}
    $stats_newestuser = $newestmember;
	// Format the stats language.
	$stats_posts = my_number_format($stats['numposts']);
    $stats_threads = my_number_format($stats['numthreads']);
	$stats_numusers = my_number_format($stats['numusers']);

	// Find out what the highest users online count is.
	$mostonline = $cache->read("mostonline");
	if ($onlinecount > $mostonline['numusers'])
	{
		$time = TIME_NOW;
		$mostonline['numusers'] = $onlinecount;
		$mostonline['time'] = $time;
		$cache->update("mostonline", $mostonline);
	}
	$recordcount = $mostonline['numusers'];
	$recorddate = my_date($dsb->settings['dateformat'], $mostonline['time']);
	$recordtime = my_date($dsb->settings['timeformat'], $mostonline['time']);


	eval("\$forumstats = \"" . $templates->get("index_stats") . "\";");
}

// Show the board statistics table only if one or more index statistics are enabled.
if ($dsb->settings['showwol'] != 0 || $dsb->settings['showindexstats'] != 0 || ($dsb->settings['showbirthdays'] != 0 && $bdaycount > 0))
{
	if (!is_array($stats))
	{
		// Load the stats cache.
		$stats = $cache->read("stats");
	}
    if(!$legend) $legend = "";
	eval("\$boardstats = \"" . $templates->get("index_boardstats") . "\";");
}

if ($dsb->user['user_id'] == 0)
{
	// Build a forum cache.
	$query = dbquery("
		SELECT f.*, f2.*
        FROM " . DB_FORUMS . " f
        LEFT JOIN " . DB_DSB_FORUMS . " f2 ON f2.fid=f.forum_id
		WHERE f2.active != 0
		ORDER BY pid, forum_order
	");

	$forumsread = unserialize($dsb->cookies['forumread']);
}
else
{
	// Build a forum cache.
	$query = dbquery("
		SELECT f.*, f2.*, fr.dateline AS lastread
		FROM " . DB_FORUMS . " f
        LEFT JOIN " . DB_DSB_FORUMS . " f2 ON f2.fid=f.forum_id
		LEFT JOIN " . DB_DSB_FORUMSREAD . " fr ON fr.fid=f.forum_id AND fr.uid='" . $dsb->user['user_id'] . "'
        WHERE f2.active != 0
		ORDER BY pid, forum_order
	");
}
while ($forum = dbarray($query))
{
	if ($dsb->user['user_id'] == 0)
	{
		if ($forumsread[$forum['fid']])
		{
			$forum['lastread'] = $forumsread[$forum['forum_id']];
		}
	}
	$fcache[$forum['pid']][$forum['forum_order']][$forum['fid']] = $forum;
}
$forumpermissions = forum_permissions();
// Decide if we're showing first-level subforums on the index page.
if ($dsb->settings['subforumsindex'] != 0)
{
	$showdepth = 3;
}
else
{
	$showdepth = 2;
}
$forum_list = build_forumbits();
$forums = opentable($locale['discussion_forum']) . $forum_list['forum_list'] . closetable();
$forums .= $boardstats;

output_page($forums);

/*
echo "<table>";
echo "<tr><td colspan='2' style='border-bottom: 1px solid #ddd;border-top: 1px solid #ddd;'>USERDATA</td></tr>";
foreach ($dsb->user as $key => $val) {
echo "<tr><td>{$key}</td><td>{$val}</td></tr>";
}
echo "</table><br />";
echo "<table width='100%'>";
echo "<tr><td colspan='2' style='border-bottom: 1px solid #ddd;border-top: 1px solid #ddd;'>SETTINGS</td></tr>";
foreach ($dsb->settings as $key => $val) {
echo "<tr><td width='1%'>{$key}</td><td>{$val}</td></tr>";
}
echo "</table>";
*/

?>